package com.fingard.dsp.bank.directbank.payfree01;

import java.util.HashMap;
import java.util.Map;

import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpPortClient;
import com.fingard.net.TcpReceiver;
import com.fingard.text.StringHelper;
import com.fingard.util.SortHelper;
import com.fingard.xml.XmlTextWriter;


public class PAYFREE01Base extends DirectBase implements ITcpClient {
	
	
	protected void writeCommonHeader(XmlTextWriter xmlWriter) throws Exception{
		
		xmlWriter.writeStartDocument();
		
	}
	
	protected String getCharset(){
		return getBankConfig().getCharset("GBK");
	}
	
	protected String getCharset(String p_def, String p_for){
		return getBankConfig().getCharset(p_def, p_for);
	}
	
	public String getCharset(String p_for) {
		return getBankConfig().getCharset("GBK", p_for);
	}
	
	protected String[] sendToBank(String p_reqStr) throws Exception{
		
		String[] retStr = new String[]{"",""};
		
		//if(bankFront.org == null || bankFront.org.length() == 0){
		//	throw new Exception("CIS不能为空");
		//}
		
		WriteBankLogLn("2.提交银行的地址："+bankFront.serverIP+":"+bankFront.serverPort);
		
		//发送银行前置
		TcpPortClient tmpClient = new TcpPortClient(bankFront.serverIP,Integer.parseInt(bankFront.serverPort),this);
		String tmpCharset = "GBK";
		byte[] tmpBytesBody = p_reqStr.getBytes(tmpCharset);
		String tmpStrLen = String.format("% 8d", tmpBytesBody.length);
		String tmpHead = "Content-Length:" + tmpStrLen + "\r\n";
		byte[] tmpBytesHead = tmpHead.getBytes(tmpCharset);
		
		byte[] tmpBytesSend = new byte[tmpBytesHead.length + tmpBytesBody.length];
		System.arraycopy(tmpBytesHead, 0, tmpBytesSend, 0, tmpBytesHead.length);
		System.arraycopy(tmpBytesBody, 0, tmpBytesSend, tmpBytesHead.length, tmpBytesBody.length);
		
		WriteBankLogStep2(tmpHead + p_reqStr);
		TcpReceiver tmpReceiver = tmpClient.sendMessage(tmpBytesSend);
		if(StringHelper.isNullOrEmpty(tmpReceiver.connState)){
			String tmpBkStr = new String(tmpReceiver.recBytes,0, tmpReceiver.readedLength, tmpCharset);
			WriteBankLogStep3(tmpBkStr);
		}
		retStr[0] = tmpReceiver.connState;
		if(tmpReceiver.isCompleted){
			retStr[1] = new String(tmpReceiver.recBytes, tmpReceiver.contentStartIndex,tmpReceiver.contentLength,tmpCharset);
		}
		else{
			retStr[1] = tmpReceiver.getConnMsg();
			WriteBankLogLn(retStr[1]);
		}
        return retStr;
	}
	
	public void getContentLength(TcpReceiver p_receiver) throws Exception{
		byte tmpByteLt = "<".getBytes("GBK")[0];
		if(p_receiver.contentLength == -1){
			for(int i=0;i<p_receiver.readedLength;i++){
				if(p_receiver.recBytes[i]== tmpByteLt){
					if(p_receiver.contentStartIndex<=0){
						p_receiver.contentStartIndex = i;
					}
					String tmpHeadStr = new String(p_receiver.recBytes,0,i,"GBK");
					String[] tmpKeyValues = tmpHeadStr.split("\n");
					for(int j=0;j<tmpKeyValues.length;j++){
						String[] tmpEach = tmpKeyValues[j].split(":");
						if(tmpEach[0].toLowerCase().equals("content-length")){
							p_receiver.contentLength = Integer.valueOf(tmpEach[1].trim());
						}
					}
					break;
				}
			}
		}
	}
	
	public Map<String, String> removeHead(Map<String, String> tmpMapParams) {
		tmpMapParams.remove("BankCode");
		tmpMapParams.remove("TransType");
		tmpMapParams.remove("CommandCode");
		tmpMapParams.remove("Entity");
		return tmpMapParams;
	}
	
	protected String getSignStr(Map<String, String> mapParams) {
		Map<String, String> signMap = new HashMap<String, String>();

		for (Map.Entry<String, String> entry : mapParams.entrySet()) {
			String name = entry.getKey();
			String value = entry.getValue();

			if (StringHelper.hasAnyChar(value)) {
				signMap.put(name, value);
			}
		}

		// Map排序
		signMap = SortHelper.sortMapByName(signMap);

		// Map转HTTP字符串
		String signStr = "";
		for (String key : signMap.keySet()) {
			signStr += "&" + key + "=" + signMap.get(key);
		}
		signStr = signStr.substring(1);
		
		return signStr;
	}
}
